package servlet;

import dao.ManagerDAOImpl;
import pojo.Manager;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/managergg")
public class ManagerServlet extends BaseServlet {

    // 添加经理
    protected void addManager(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int managerID = Integer.parseInt(req.getParameter("managerID"));
        String idNumber = String.valueOf(req.getParameter("idNumber"));

        Manager manager = new Manager(managerID, idNumber);
        ManagerDAOImpl.add(manager);
        sendJsonResponse(resp, new ApiResult(200, "Manager added successfully.", null));
    }

    // 删除经理
    protected void deleteManager(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int managerID = Integer.parseInt(req.getParameter("managerID"));
        ManagerDAOImpl.delete(managerID);
        sendJsonResponse(resp, new ApiResult(200, "Manager deleted successfully.", null));
    }

    // 更新经理
    protected void updateManager(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int managerID = Integer.parseInt(req.getParameter("managerID"));
        String idNumber = String.valueOf(req.getParameter("idNumber"));
        Manager manager = new Manager(managerID, idNumber);
        ManagerDAOImpl.update(manager);
        sendJsonResponse(resp, new ApiResult(200, "Manager updated successfully.", null));
    }

    // 获取所有经理
    protected void getAllManagers(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Manager> managers = ManagerDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", managers));
    }

    // 根据编号获取经理
    protected void getManagerById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int managerID = Integer.parseInt(req.getParameter("managerID"));
        Manager manager = ManagerDAOImpl.getById(managerID);
        sendJsonResponse(resp, new ApiResult(200, "Success", manager));
    }

    // 获取经理数量
    protected void getManagerCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = ManagerDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取经理
    protected void getManagersByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Manager> managers = ManagerDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", managers));
    }

    // 根据关键字搜索经理
    protected void findManagersByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<Manager> managers = ManagerDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", managers));
    }

    // 根据关键字分页搜索经理
    protected void findManagersByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Manager> managers = ManagerDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", managers));
    }
}
